package chat.network.http;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.xkh.xkh.MyApplication;
import com.xkh.xkh.R;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import inter.Delegate;
import managers.VolleySingleton;

/**
 * Created by 凯 on 2015/10/23.
 */
public class GetAllRooms {
    Delegate delegate;
    String user_ID;

    public void setUser_ID(String user_ID) {
        this.user_ID = user_ID;
    }

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void get() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        final String url = "http://" + IP + "/Chat/GetAllRooms";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (delegate != null)
                            delegate.onResponse(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("user_ID",user_ID);
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }


    public static class UserRooms implements Serializable{
        private String join_Role;
        private Long room_ID;
        private String room_Name;
        public String getJoin_Role() {
            return join_Role;
        }
        public void setJoin_Role(String join_Role) {
            this.join_Role = join_Role;
        }

        public Long getRoom_ID() {
            return room_ID;
        }
        public void setRoom_ID(Long room_ID) {
            this.room_ID = room_ID;
        }

        public String getRoom_Name() {
            return room_Name;
        }
        public void setRoom_Name(String room_Name) {
            this.room_Name = room_Name;
        }
    }

    public static class ResponseRooms {
        private String status;
        private List<UserRooms> userRooms;

        public String getStatus() {
            return status;
        }
        public void setStatus(String status) {
            this.status = status;
        }

        public List<UserRooms> getUserRooms() {
            return userRooms;
        }
        public void setUserRooms(List<UserRooms> userRooms) {
            this.userRooms = userRooms;
        }
    }


}
